import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
/*
 * ConstraintsDialog.java
 *
 * $LastChangedDate: 2008-05-29 11:00:51 +0000 (Thu, 29 May 2008) $
 * $LastChangedRevision: 38 $
 * Vicente J. Ferrer Dalmau
 * < vicente@jdalmau.es >
 *
 */

/**
 *
 * @author Vicente J. Ferrer Dalmau
 */
public class ConstraintsDialog extends JDialog {
    
    Map MAP;
    JScrollPane jScrollPane1;
    JList jList1;
    JLabel jLabel3, jDjLZone;
    JButton jButton2, jButton3;
    
    /** Creates a new instance of ConstraintsDialog */
    public ConstraintsDialog() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jDjLZone = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        
        setTitle("Restricciones para la Zona");
        setMinimumSize(new java.awt.Dimension(300, 310));
        setModal(true);
        setResizable(false);
        
        jScrollPane1.setViewportView(jList1);

        jLabel3.setText("Zona:");

        jDjLZone.setText("jLabel4");

        jButton2.setText("Aplicar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Cancelar");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel3)
                        .addGap(14, 14, 14)
                        .addComponent(jDjLZone))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jDjLZone))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }
    
    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {

        Object[] selectedValues = jList1.getSelectedValues();
        
        // get the zone that called the JDialog
        Zone zone = MAP.zones[Integer.parseInt(jDjLZone.getText())];

        // apply the selected constraints        
        zone.addConstraints(selectedValues);
        
        setVisible(false);
        MAP.revalidate();
        MAP.repaint();        
    }
    
    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      

        setVisible(false);
    }
    
    /** Sets a related Map for this Dialog
     */
    public void setMap (Map map) {
        
        MAP = map;
    }
    
    /** Loads all the colors of its related Map
     */
    public void loadColors () {

        // init the JList
        DefaultListModel model = new DefaultListModel();
        for (int i=0; i<MAP.COLORS; i++)
            model.addElement(ColorsList.mycolors[i]);

        jList1.setModel(model);        
    }
    
    /** Sets the number of the Zone on the Dialog
     */
    public void setZone (Zone zone) {
       
        jDjLZone.setText(Integer.toString(zone.NUMBER));
    }

    /** Shows the Dialog for the specified zone
     */
    public void show (Zone zone) {
        
        setZone(zone);
        
        // highlight the available colors for the Zone (if they're not all -> has constraints)
        jList1.clearSelection();
        if (zone.constraints.size() != 0) {
            int[] toHighlight = new int[zone.constraints.size()];
            for (int i=0; i<zone.constraints.size(); i++)
                toHighlight[i] = ((MyColor)zone.constraints.get(i)).position;

            jList1.setSelectedIndices(toHighlight);    
        }
        
        setVisible(true);
    }

}